import os,shutil,function
import mysql.connector
from datetime import datetime

mydb = mysql.connector.connect(
  host="192.168.10.81",
  user="root",
  passwd="",
  database="xiaocheng"
)
# 获取最大id
mycursor = mydb.cursor()
mycursor.execute("select max(id) from multipack_model") 
myresult = mycursor.fetchone()
begin_id=myresult[0]+1


# 读取数据
dir = 'D:/Desk/多件装上传'
paths = function.get_file_paths(dir)
i=0
datas=[]
for path in paths:
    dict=[] 
    name='MD' +str(begin_id + 1000 + i)
    pic=name+os.path.splitext(path)[1]
    new_path="Y:/pic/multipack/"+pic
    shutil.copy(path,new_path)
    file_name=os.path.basename(path)
    style_name=file_name.split('&&')[0]
    dict.append(name)
    dict.append(pic)
    dict.append(style_name)
    i+=1
    datas.append(tuple(dict))

print(i)



# 插入数据
sql = "insert into multipack_model (name,pic,style_name) values (%s,%s,%s)" 
mycursor.executemany(sql, datas) 
mydb.commit()    # 数据表内容有更新，必须使用到该语句
 
print(mycursor.rowcount, "记录插入成功。")

mycursor.close()
mydb.close()